package ihm.manes_ho.agenda.dialog;

import ihm.manes_ho.agenda.R;
import ihm.manes_ho.agenda.vo.MeetingVO;
import android.app.Dialog;
import android.content.Context;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

/**
 * RendezVousDialog is showed when users perform a "add/update a rendez-vous"
 */
public class RendezVousDialog extends Dialog {
	private EditText txtName;
	private EditText txtAddress;
	private EditText txtHour_start;
	private EditText txtMinute_start;
	private EditText txtHour_end;
	private EditText txtMinute_end;
	private Button btnCancel;
	private Button btnOk;
	private Spinner spChooseWeekdays;
	private int weekday_index;
	private int type;
	private OnOkClickListener onOkListener;
	
	public RendezVousDialog(Context context, int type, OnOkClickListener callBack) {
		super(context);
		this.onOkListener = callBack;
		this.type = type;
		
		initComponents();
	}
	
	private void initComponents() {
		setContentView(R.layout.dialog_rendez_vous);
		setTitle(type == 0 ? R.string.txt_meeting_add : R.string.txt_meeting_edit);
		
		initEditTexts();

		initCancelButton();

		initOkButton();
		
		initWeekdaySpinner();
	}
	
	private void initEditTexts() {
		//name
		txtName = (EditText) this.findViewById(R.id.txt_meeting_name);		

		//address
		txtAddress = (EditText) this.findViewById(R.id.txt_meeting_address);
		
		//time - start hour
		txtHour_start = (EditText) this.findViewById(R.id.txt_meeting_hour_start);
		//time - start minute
		txtMinute_start = (EditText) this.findViewById(R.id.txt_meeting_minute_start);
		//time - end hour
		txtHour_end = (EditText) this.findViewById(R.id.txt_meeting_hour_end);
		//time - end minute
		txtMinute_end = (EditText) this.findViewById(R.id.txt_meeting_minute_end);
	}
	
	private void initCancelButton() {
		btnCancel = (Button) this.findViewById(R.id.button_cancel);
		btnCancel.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				RendezVousDialog.this.dismiss(); //dismiss();
				RendezVousDialog.this.clear(); //clear();
			}
		});
	}
	
	private void initOkButton() {
		//ok button
		btnOk = (Button) this.findViewById(R.id.button_ok);
		btnOk.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				if (onOkListener != null) {
					
					//calls the callback method
					onOkListener.onOkClick(getType(), getName(), getAddress(), getWeekday_index(), getTimeStart(), getTimeEnd());
					
					RendezVousDialog.this.dismiss(); //dismiss();
					RendezVousDialog.this.clear(); //clear();
				}
			}
		});
	}
	
	private void initWeekdaySpinner() {
		spChooseWeekdays = (Spinner) this.findViewById(R.id.spinner_weekdays);
		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
				this.getContext(), R.array.weekdays_fullname, android.R.layout.simple_spinner_item);

		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

		spChooseWeekdays.setAdapter(adapter);

		spChooseWeekdays.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int position, long id) {
				RendezVousDialog.this.weekday_index = position;
			}
			
			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				Log.v("spinner>onNothingSelected", "nothing...");        		
			}
		});  

	}
	
	private int getType() {
		return type;
	}
	
	private String getName() {
		return txtName.getText().toString();
	}
	
	private String getAddress() {
		return txtAddress.getText().toString();
	}
	
	private int getWeekday_index() {
		return weekday_index;
	}
	
	private int getTimeStart() {
		int hour = Integer.valueOf(txtHour_start.getText().toString());
		int minute = Integer.valueOf(txtMinute_start.getText().toString());
		
		return hour * 60 + minute;
	}
	
	private int getTimeEnd() {
		int hour = Integer.valueOf(txtHour_end.getText().toString());
		int minute = Integer.valueOf(txtMinute_end.getText().toString());
		
		return hour * 60 + minute;
	}
	
	private void clear() {
		txtName.setText("");
		txtAddress.setText("");
		txtHour_start.setText("00");
		txtMinute_start.setText("00");
		txtHour_end.setText("00");
		txtMinute_end.setText("00");
	}
	
	public void setData(MeetingVO rendez_vous) {
		txtName.setText(rendez_vous.getName());
		txtAddress.setText(rendez_vous.getAddress());
		txtHour_start.setText((rendez_vous.getTimeStart() / 60) + "");
		txtMinute_start.setText((rendez_vous.getTimeStart() % 60) + "");
		txtHour_end.setText((rendez_vous.getTimeEnd() / 60) + "");
		txtMinute_end.setText((rendez_vous.getTimeEnd() % 60) + "");
		spChooseWeekdays.setSelection(rendez_vous.getWeekday_index());
		weekday_index = rendez_vous.getWeekday_index();
		spChooseWeekdays.setEnabled(false);
	}
	
	/**
	 * OnOkClickListener
	 */
	public static interface OnOkClickListener {

		/**
		 * callBack method - this method will be called when the Ok button is clicked
		 * @param type type of either action add or edit. type = 0 if action is 'add', type = 1 if action is 'edit'
		 * @param name meeting's name
		 * @param address meeting's address
		 * @param weekday_index
		 * @param timeStart meeting's starting time
		 * @param timeEnd meeting's ending time
		 */
		void onOkClick(int type, String name, String address, int weekday_index, int timeStart, int timeEnd);
	}
}
